//
//  SearchResultCell.m
//  Grammar
//
//  Created by Rick on 6/28/13.
//
//

#import "SearchResultCell.h"
#import "Grammar.h"
#import "TagManager.h"
#import "Tag.h"

@interface SearchResultCell()
@property (retain, nonatomic) IBOutlet UILabel *categoryLabel;
@property (retain, nonatomic) IBOutlet UILabel *countLabel;
@property (retain, nonatomic) IBOutlet UILabel *titleLabel;
@property (retain, nonatomic) IBOutlet UILabel *tagLabel;
@property (retain, nonatomic) IBOutlet UIScrollView *tagsView;

@end

@implementation SearchResultCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)tapMoreOptionButton:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(tapMoreOption:withIndex:)]) {
        [_delegate tapMoreOption:_currentGrammar withIndex:_cellRow];
    }
}

- (void)setCellRow:(NSInteger)cellRow
{
    _cellRow = cellRow;
}
- (void)setCellWithGrammar:(Grammar*)grammar
{
//
    _currentGrammar = [grammar retain];
    _titleLabel.text = grammar.title;
    _titleLabel.text = grammar.title;
    _countLabel.text = [NSString stringWithFormat:@"[%@]条解释",grammar.explainCount];
//    CGSize fitLabelSize = CGSizeMake(320, 14);
    NSString *tagString = [[TagManager sharedManager] getTagWithid:grammar.grammarTagId].tagName;
    _categoryLabel.text = tagString;
//    CGSize labelSize = [tagString sizeWithFont:self.tagLabel.font constrainedToSize:fitLabelSize];
//    _tagLabel.text = tagString;
//    _tagLabel.width = labelSize.width;
    _tagLabel.hidden = YES;
    
    [_tagsView removeAllSubviews];
    
    CGFloat origientX = 0.0;
    for (NSDictionary *tmpString in grammar.keyWords) {
        CGSize fitLabelSize = CGSizeMake(320, 14);
        CGSize labelSize = [[tmpString objectForKey:@"name"] sizeWithFont:self.tagLabel.font constrainedToSize:fitLabelSize];
        origientX += 5;
        UILabel *tagLabel = [[[UILabel alloc] initWithFrame:(CGRectMake(origientX, 0, labelSize.width+20, 14))] autorelease];
        tagLabel.backgroundColor = [UIColor colorWithRed:141/255.0 green:196/255.0 blue:204/255.0 alpha:1.0];
        tagLabel.textColor = [UIColor whiteColor];
        tagLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
        tagLabel.text = [tmpString objectForKey:@"name"];
        tagLabel.textAlignment = UITextAlignmentCenter;
        origientX += tagLabel.width;
        [_tagsView addSubview:tagLabel];
    }
    [_tagsView setContentSize:CGSizeMake(origientX, 14)];
}

- (void)dealloc {
    [_currentGrammar release];
    [_categoryLabel release];
    [_countLabel release];
    [_titleLabel release];
    [_tagLabel release];
    [_tagsView release];
    [_moreOptionButton release];
    [super dealloc];
}
@end
